using System.Collections;
using System.Collections.Generic;
using XDFLib.Collections;

namespace NodeBasedSkillEditor
{
    public class RT_DAG
    {
        public Dictionary<uint, RT_Node> AllNodes { get; private set; } = new Dictionary<uint, RT_Node>();
        public BiSet<uint> Links { get; private set; } = new BiSet<uint>();
        public RT_Node EntryNode { get; private set; }

        public RT_DAG(DAG_Data data)
        {
            foreach (var l in data.Links)
            {
                foreach (var destId in l.Value)
                {
                    Links.Add(l.Key, destId);
                }
            }

            foreach (var n in data.AllNodes)
            {
                var nodeData = n.Value;
                var rtNode = nodeData.BuildRuntimeNode(this);
                AllNodes.Add(n.Key, rtNode);
                if (rtNode.Id == data.EntryNodeId)
                {
                    EntryNode = rtNode;
                }
            }

            foreach (var n in AllNodes)
            {
                n.Value.InitializePortLinks();
            }
        }

        public RT_Node GetNode(uint nodeId)
        {
            RT_Node result;
            AllNodes.TryGetValue(nodeId, out result);
            return result;
        }

        public RT_Port GetPort(uint portId)
        {
            uint nodeId = DAGHelper.GetNodeIdFromPortGlobalId(portId);
            uint portLocalId = DAGHelper.PortGlobalId_To_PortLocalId(portId);

            var node = GetNode(nodeId);
            if (node != null)
            {
                return node.GetPort(portLocalId);
            }
            return null;
        }

    }

}
